package com.geek.factory.service.strategy;

import com.geek.factory.exception.FactoryException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author chenmin
 * @desc 文件存储管理的上下文
 * @date 2025/6/24
 */
@Service
public class FileUploadContext {

    //键 -> 策略在IOC容器中的beanName   值 -> 策略
    private final Map<String , UploadStrategy> strategies;

    @Autowired
    public FileUploadContext(Map<String , UploadStrategy> strategies){
        this.strategies = strategies;
    }

    //上传
    public String upload(MultipartFile file , String provider){
        UploadStrategy uploadStrategy = strategies.get(provider);
        if(uploadStrategy == null){
           throw new FactoryException("未知的云服务商: "+provider);
        }
        return uploadStrategy.uploadFile(file);
    }

    //删除
    public boolean delete(String url , String provider){
        UploadStrategy uploadStrategy = strategies.get(provider);
        if(uploadStrategy == null){
            throw new FactoryException("未知的云服务商: "+provider);
        }
        return uploadStrategy.deleteFile(url);
    }
}
